﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;
using MSWord = Microsoft.Office.Interop.Word;

namespace MicroWordDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnConvertChooseFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "word files (*.doc)|*.docx";
            var dialogResult = openFileDialog.ShowDialog();
            if (dialogResult == DialogResult.OK)
            {
                string path = openFileDialog.FileName;
                txtConvertFile.Text = path;
                txtConvertFile.Tag = openFileDialog.SafeFileName;
            }
        }

        private void txtConvertFile_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog()
            {
                Title = "选择水印图片",
                Filter = "图像文件(*.jpg;*.gif;*.png)|*.jpg;*.gif;*.png",
            };

            var dialogResult = openFileDialog.ShowDialog();
            if (dialogResult == DialogResult.OK)
            {
                string path = openFileDialog.FileName;
                pictureBox1.ImageLocation = path;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            object path= txtConvertFile.Text;                              //文件路径变量
            string strContent;                        //文本内容变量
            MSWord.Application wordApp;                   //Word应用程序变量 
            MSWord.Document wordDoc;                  //Word文档变量
            object unknow = Type.Missing;
            wordApp = new MSWord.ApplicationClass(); //初始化
            wordDoc = wordApp.Documents.Open(ref path,
 ref unknow, ref unknow, ref unknow, ref unknow, ref

 unknow,
 ref unknow, ref unknow, ref unknow, ref unknow, ref

 unknow,
 ref unknow, ref unknow, ref unknow, ref unknow, ref

 unknow);

            //foreach (Paragraph p in wordDoc.Paragraphs)
            //{
            //    var text = p.Range.Text.Trim();
            //}

            foreach (Page pge in wordDoc.Application.ActiveDocument.ActiveWindow.Panes[1].Pages)
            {

                //write codes here
            }

            object oMissing = System.Reflection.Missing.Value;
            object what = MSWord.WdGoToItem.wdGoToPage;
            object which = MSWord.WdGoToDirection.wdGoToAbsolute;
            object count = 0;

            Range range1 = wordApp.Selection.GoTo(ref what, ref which, ref count, ref oMissing);

            wordDoc.Application.ActiveDocument.InlineShapes.AddPicture(pictureBox1.ImageLocation);

            object count2 = 2;
            Range range2 = wordApp.Selection.GoTo(ref what, ref which, ref count2, ref oMissing);

            wordDoc.Application.ActiveDocument.InlineShapes.AddPicture(pictureBox1.ImageLocation);



        }
    }
}
